<template>
  <div class="main-container">
    <MyHeader title="商品列表页"></MyHeader>
    <MyTable :list="list">
      <template #head>
        <th>编号</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="scope">
        <td>{{ scope.row.id }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <button
            v-if="!scope.row.inputVisible"
            @click="scope.row.inputVisible = true"
            type="button"
            class="btn btn-primary"
          >
            +Tag</button
          ><br />
          <input
            @keydown.esc="scope.row.inputValue=''"
            @keydown.enter="inputEnter(scope.row)"
            v-model="scope.row.inputValue"
            @blur="scope.row.inputVisible = false"
            v-foc
            v-if="scope.row.inputVisible"
            type="text"
          />
          <span
            style="margin-left: 5px"
            class="badge bg-warning text-dark"
            v-for="(item, index) in scope.row.tags"
            :key="index"
            >{{ item }}</span
          >
        </td>
        <td>
          <button @click="delFn(scope.row.id)" class="btn btn-sm btn-danger">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyHeader from "../components/MyHeader";
import MyTable from "../components/MyTable";
export default {
  name: "Mygoodslist",
  components: {
    MyTable,
    MyHeader,
  },
  async created() {
    let res = await this.$http({ url: "/api/goods" });
    //  console.log(res);
    this.list = res.data.data;
  },
  data() {
    return {
      list: [],
    };
  },

  mounted() {},

  methods: {
    delFn(i) {
      this.list = this.list.filter((ele) => ele.id != i);
    },
    inputEnter(obj) {
      if (obj.inputValue.trim() === "") {
        alert("请输入内容");
      }
      obj.tags.push(obj.inputValue);
      obj.inputValue = "";
    },
  },
};
</script>

<style lang="less" scoped>
.main-container {
  padding-top: 45px;
}
</style>